package com.europe.business.europebusiness.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.support.v7.widget.DividerItemDecoration;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;
import android.widget.TextView;

import com.europe.business.europebusiness.R;
import com.europe.business.europebusiness.ui.activity.base.ActionBarActivity;
import com.europe.business.europebusiness.ui.adapter.ListAdapter;
import com.europe.business.europebusiness.ui.adapter.viewholder.ListViewHolder;
import com.europe.business.europebusiness.ui.bean.CompanyCategoryBean;
import com.europe.business.europebusiness.ui.common.Commons;
import com.europe.business.europebusiness.ui.net.ICompany;
import com.europe.business.europebusiness.ui.utils.Utils;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class SearchActivity extends ActionBarActivity implements ListAdapter.ItemClicked {


    private RecyclerView recyclerView;
    List<CompanyCategoryBean.Data> list;

    @Override
    protected int setMainContent() {
        return R.layout.activity_info_select_list;
    }

    @Override
    protected void initView() {
        super.initView();
        recyclerView = findViewById(R.id.rv);
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        recyclerView.addItemDecoration(new DividerItemDecoration(this, DividerItemDecoration.VERTICAL));
        setSearchVisible(true, new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });
    }

    @Override
    protected void initData() {
        super.initData();
        getData();
    }

    @Override
    protected TextWatcher getTextWatcher() {
        TextWatcher textWatcher = new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        };
        return textWatcher;
    }

    private void getData(){
        Retrofit retrofit = new Retrofit.Builder()
                .addConverterFactory(GsonConverterFactory.create())
                .baseUrl(Commons.SERVER_URL)
                .build();
        ICompany company = retrofit.create(ICompany.class);
        Call<CompanyCategoryBean> call = company.getCompanyCategory(1,20,Utils.getSystemLanguage(this));
        call.enqueue(new Callback<CompanyCategoryBean>() {
            @Override
            public void onResponse(Call<CompanyCategoryBean> call, Response<CompanyCategoryBean> response) {
                CompanyCategoryBean bean = response.body();
                if(null != bean){
                    if(bean.getErrorCode() == 0){
                        list = bean.getData();
                        recyclerView.setAdapter(new ListAdapter<CompanyCategoryBean.Data>(SearchActivity.this, list, R.layout.activity_text_item, SearchActivity.this) {
                            @Override
                            public void convert(ListViewHolder holder, CompanyCategoryBean.Data data) {
                                TextView tv = holder.getView(R.id.tv);
                                tv.setText(data.getCategory());
                            }
                        });
                    }else{
                        showToastLong(bean.getErrorMessage());
                    }
                }else{
                    showToastLong(getString(R.string.requst_failed));
                }

            }

            @Override
            public void onFailure(Call<CompanyCategoryBean> call, Throwable t) {
                Log.i("ListActivity", t.getMessage());
                showToastLong(getString(R.string.requst_failed));
            }
        });
    }

    @Override
    public void onItemClicked(int index) {

        if(null != list){
            String category = list.get(index).getCategory();
            Intent intent = new Intent();
            intent.putExtra("data", category);
            setResult(Activity.RESULT_OK, intent);
            finish();
        }

    }
}
